package main.parsers.utils;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import main.database.Database;
import main.parsers.AbstractParser;
import main.parsers.matches.FieldStat;
import main.parsers.matches.GateStat;

public abstract class Stats extends AbstractParser 
{
	private final static int MAX_ROWS = 30;
	
	private Stats(Database db)
	{
		super(db);
	}
	
	public static List<FieldStat> bestBombardiersForwards()
	{
		return getPlayerStat("best.bombardiers.forwards");
	}
	
	public static List<FieldStat> bestBombardiersDefenders()
	{
		return getPlayerStat("best.bombardiers.defenders");
	}
	
	public static List<GateStat> bestGoalkeepersSaves()
	{
		return getGoalkeeperStat("best.goalkeepers.saves");
	}
	
	public static List<FieldStat> bestSnipersForwards()
	{
		return getPlayerStat("best.snipers.forwards");
	}
	
	public static List<FieldStat> bestSnipersDefenders()
	{
		return getPlayerStat("best.snipers.defenders");
	}
	
	public static List<GateStat> bestGoalkeepersSafety()
	{
		return getGoalkeeperStat("best.goalkeepers.safety");
	}
	
	public static List<FieldStat> bestAssistentsForwards()
	{
		return getPlayerStat("best.assistents.forwards");
	}
	
	public static List<FieldStat> bestAssistentsDefenders()
	{
		return getPlayerStat("best.assistents.defenders");
	}
	
	public static List<GateStat> bestGoalkeepersWhites()
	{
		return getGoalkeeperStat("best.goalkeepers.whites");
	}
	
	public static List<FieldStat> bestUtility()
	{
		return getPlayerStat("best.utility");
	}
	
	public static List<FieldStat> bestWinpucks()
	{
		return getPlayerStat("best.winpucks");
	}
	
	public static List<FieldStat> bestPenalty()
	{
		return getPlayerStat("best.penalty");
	}
	
	public static List<Standing> topChampionship()
	{
		return getClubStat("top.clubs.championship", null);
	}
	
	public static List<Standing> topConference(int id)
	{
		return getClubStat("top.clubs.conference", id);
	}
	
	public static List<Standing> topDivision(int id)
	{
		return getClubStat("top.clubs.division", id);
	}
	
	private static List<FieldStat> getPlayerStat(String queryName, Integer id)
	{
		List<FieldStat> stats = new ArrayList<FieldStat>(MAX_ROWS);
		ResultSet rs = null;
		Object[] params = (id != null) ? new Object[] { id } : null;
		try
		{
			rs = Database.getInstance().executeQuery(queryName, params);
			while (rs.next()) stats.add(FieldStat.get(rs));
		}
		catch (Exception e)
		{
			System.err.println("Unable to get player stats from database");
			e.printStackTrace();
		}
		finally
		{
			closeResultSet(rs);
		}
		
		return stats;
	}
	
	private static List<GateStat> getGoalkeeperStat(String queryName, Integer id)
	{
		List<GateStat> stats = new ArrayList<GateStat>(MAX_ROWS);
		ResultSet rs = null;
		Object[] params = (id != null) ? new Object[] { id } : null;
		try
		{
			rs = Database.getInstance().executeQuery(queryName, params);
			while (rs.next()) stats.add(GateStat.get(rs));
		}
		catch (Exception e)
		{
			System.err.println("Unable to get goalkeeper stats from database");
			e.printStackTrace();
		}
		finally
		{
			closeResultSet(rs);
		}
		
		return stats;
	}
	
	private static List<FieldStat> getPlayerStat(String queryName)
	{
		return getPlayerStat(queryName, null);
	}
	
	private static List<GateStat> getGoalkeeperStat(String queryName)
	{
		return getGoalkeeperStat(queryName, null);
	}
	
	private static List<Standing> getClubStat(String queryName, Integer id)
	{
		List<Standing> stats = new ArrayList<Standing>();
		ResultSet rs = null;
		Object[] params = (id != null) ? new Object[] { id } : null;
		try
		{
			rs = Database.getInstance().executeQuery(queryName, params);
			while (rs.next()) stats.add(Standing.get(rs));
		}
		catch (Exception e)
		{
			System.err.println("Unable to get standings from database");
			e.printStackTrace();
		}
		finally
		{
			closeResultSet(rs);
		}
		
		return stats;
	}
	
	public static List<FieldStat> getPlayerStat(Integer id)
	{
		return getPlayerStat("select.stat.player", id);
	}
	
	public static List<GateStat> getGoalkeeperStat(Integer id)
	{
		return getGoalkeeperStat("select.stat.player", id);
	}
	
	public static List<GateStat> getGateStats(Integer match)
	{
		return getGoalkeeperStat("select.gatestats.match", match);
	}
	
	public static List<FieldStat> getFieldStats(Integer match)
	{
		return getPlayerStat("select.fieldstats.match", match);
	}

}
